package com.qiche.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qiche.aspect.Result;
import com.qiche.mapper.CardSubsidyMapper;
import com.qiche.mapper.MerchantMapper;
import com.qiche.mapper.WalletLogMapper;
import com.qiche.mapper.WalletMapper;
import com.qiche.model.CardSubsidy;
import com.qiche.model.Merchant;
import com.qiche.model.Wallet;
import com.qiche.requestpram.CardSubsidyParam;
import com.qiche.service.CardSubsidyService;

import tk.mybatis.mapper.entity.Example;

/**
 * @author LB
 *
 * @date 2018年5月18日
 */
@Service("cardSubsidyService")
public class CardSubsidyServiceImpl extends BaseService<CardSubsidy>  implements CardSubsidyService {

	
	private static final Logger log = LoggerFactory.getLogger(CardSubsidyServiceImpl.class);
	
	@Autowired
	private CardSubsidyMapper cardSubsidyMapper;
	
	@Autowired
	private WalletMapper walletMapper;
	
	@Autowired 
	private MerchantMapper merchantMapper;
	
	
	@Override
	 @Transactional(rollbackFor = Exception.class)
	public Result<?> addCardSubsidy(CardSubsidyParam cardSubsidyParam)throws Exception{
			Example merchantExample = new Example(Merchant.class);
			Example.Criteria merchantCriteria = merchantExample.createCriteria();
			merchantCriteria.andEqualTo("piccMid", cardSubsidyParam.getMerchantId());
			Merchant merchant= merchantMapper.selectOneByExample(merchantExample);
			if(merchant==null){
				return Result.createByErrorMessage("人保中的商户不存在系统中");
			}
			CardSubsidy cardSubsidy = new CardSubsidy();
			BeanUtils.copyProperties(cardSubsidyParam,cardSubsidy);
			cardSubsidy.setCreateTime(new Date());
			if(cardSubsidyParam.getVerifiAmount()==null||cardSubsidyParam.getVerifiAmount().doubleValue()==0.00){
				cardSubsidy.setVerifiAmount(cardSubsidyParam.getCardAmount().multiply(new BigDecimal(cardSubsidyParam.getVerifiMultiple())));
			}
			cardSubsidy.setAmount(cardSubsidyParam.getCardAmount().add(cardSubsidy.getVerifiAmount()));
			Integer status=cardSubsidyMapper.insert(cardSubsidy);
			if(status>0){
			
				Example example = new Example(Wallet.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("merchantId", merchant.getId());
				Wallet wallet =walletMapper.selectOneByExample(example);
				if(wallet.getWalletBalance()!=null){
					wallet.setWalletBalance(cardSubsidy.getAmount().add(wallet.getWalletBalance()));
				}else{
					wallet.setWalletBalance(cardSubsidy.getAmount());
				}
				wallet.setUpdateTime(new Date());
				Integer walletStatus = walletMapper.updateByExample(wallet, example);
				return Result.createBySuccess("人保卡卷补贴已到钱包", walletStatus);
			}else{
				return Result.createByError();
			}
		
	}


	
	@Override
	public Result<?> selectByMerchantId() throws Exception {
		
		return null;
	}

}
